package java流相关;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * @author coderFu
 * @create 2022/3/4
 * 内存流
 */
public class StorageStream {

    public static void main(String[] args) throws IOException {

        // 内存流， 内存流实际是数组流，字符串流
        /**
         * 字节内存流 ByteArrayInputStream ，临时存放其中
         * 字符内存流 CharArrayReader
         * 字符串流 StringReader / StringWriter
         */

         // 数据从程序写出到内存中
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byteArrayOutputStream.write("java is the best language".getBytes());
        byte[] buffer = byteArrayOutputStream.toByteArray();
        byteArrayOutputStream.close();

        String s = new String(buffer,0,byteArrayOutputStream.size());
        System.out.println(s);

        // 从内存中读入到程序中

        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(buffer);

        byte[] bytes = new byte[1024];

        int len;
        while ((len = byteArrayInputStream.read(bytes)) != -1){
            System.out.println(new String(bytes,0,len));
        }
        byteArrayInputStream.close();


    }
}
